from django.shortcuts import render
from django.views import View

# 导入序列化器
from .serializers import *

# 创建日志对象
import logging

logger = logging.getLogger("member")

# Create your views here.
"""初始化会员"""
# AllinpayMemberList.objects.update_or_create(defaults={"name": "默认会员"}, id=1000000000)


class AllinpayMemberListViewSet(viewsets.ModelViewSet):
    __title__ = "会员基础信息"
    __group__ = "会员模块"
    __info__ = {"get": "查询场所会员列表或详情"}
    __doc__ = f"""
    list:\n
    <h4>会员基础信息</h4>
    retrieve:\n
    <h4>会员基础信息</h4>
    """
    http_method_names = ["get"]
    authentication_classes = [
        MyJWTAuthentication,
        BoxJWTAuthentication,
        WeChatJWTAuthentication,
        BarJWTAuthentication,
    ]
    permission_classes = [
        MyPermissions | BoxPermissions | WeChatPermissions | BarPermissions
    ]
    serializer_class = AllinpayMemberListSerializer
    queryset = AllinpayMemberList.objects.all()
    # GET 条件过滤
    filter_backends, filterset_fields = [ListDjangoFilterBackend, SearchFilter], ["pid"]
    search_fields = ["name", "idcode", "phone", "email", "openid", "localid"]

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        serializer = self.get_serializer(queryset, many=True)
        return Response({"rescode": 200, "resmsg": serializer.data})

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = self.get_serializer(instance)
        return Response({"rescode": 200, "resmsg": serializer.data})


# class AllinpayMemberConfViewSet(viewsets.ModelViewSet):
#     __title__ = "会员基础配置"
#     __group__ = "会员模块"
#     __info__ = {"get": "查询场所会员配置列表或详情", "patch": "编辑场所会员配置"}
#     __doc__ = f"""
#     list:\n
#     <h4>会员配置列表</h4>
#     retrieve:\n
#     <h4>会员配置信息</h4>
#     partial_update:\n
#     <h4>编辑场所会员配置</h4>
#     """
#     http_method_names = ["get", "patch"]
#     authentication_classes = [MyJWTAuthentication, BoxJWTAuthentication]
#     permission_classes = [MyPermissions | BoxPermissions]
#     serializer_class = AllinpayMemberConfSerializer
#     queryset = AllinpayMemberConf.objects.all()

#     filter_backends = [ListDjangoFilterBackend]
#     filterset_fields = ["pid", "name"]

#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         serializer = self.get_serializer(queryset, many=True)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def retrieve(self, request, *args, **kwargs):
#         instance = self.get_object()
#         serializer = self.get_serializer(instance)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def partial_update(self, request, *args, **kwargs):
#         if request.data.get("name"):
#             raise serializers.ValidationError({"name": "该字段禁止编辑"})
#         if request.data.get("pid"):
#             raise serializers.ValidationError({"pid": "该字段禁止编辑"})
#         if request.data.get("compare"):
#             raise serializers.ValidationError({"compare": "该字段禁止编辑"})
#         if request.data.get("value"):
#             raise serializers.ValidationError({"value": "该字段禁止编辑"})
#         instance = self.get_object()
#         partial = True
#         serializer = self.get_serializer(instance, data=request.data, partial=partial)
#         serializer.is_valid(raise_exception=True)
#         serializer.save()
#         return Response({"rescode": 200, "resmsg": serializer.data})


# class AllinpayMemberinterListViewSet(viewsets.ModelViewSet):
#     __title__ = "会员积分"
#     __group__ = "会员模块"
#     __info__ = {"get": "查询会员积分列表或详情"}
#     __doc__ = f"""
#     list:\n
#     <h4>会员积分列表</h4>
#     retrieve:\n
#     <h4>会员积分信息</h4>
#     """
#     http_method_names = ["get"]
#     authentication_classes = [MyJWTAuthentication, BoxJWTAuthentication]
#     permission_classes = [MyPermissions | BoxPermissions]
#     serializer_class = AllinpayMemberinterListSerializer
#     queryset = AllinpayMemberinterList.objects.all()

#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         serializer = self.get_serializer(queryset, many=True)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def retrieve(self, request, *args, **kwargs):
#         instance = self.get_object()
#         serializer = self.get_serializer(instance)
#         return Response({"rescode": 200, "resmsg": serializer.data})


# class AllinpayMemberinterLogsViewSet(viewsets.ModelViewSet):
#     __title__ = "会员积分日志"
#     __group__ = "会员模块"
#     __info__ = {"get": "查询会员积分日志列表或详情"}
#     __doc__ = f"""
#     list:\n
#     <h4>会员积分日志列表</h4>
#     retrieve:\n
#     <h4>会员积分日志信息</h4>
#     """
#     http_method_names = ["get"]
#     authentication_classes = [MyJWTAuthentication, BoxJWTAuthentication]
#     permission_classes = [MyPermissions | BoxPermissions]
#     serializer_class = AllinpayMemberinterLogsSerializer
#     queryset = AllinpayMemberinterLogs.objects.all()

#     filter_backends = [ListDjangoFilterBackend, SearchFilter]
#     filterset_fields = ["mid", "localid"]
#     search_fields = ["reqsn"]

#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         serializer = self.get_serializer(queryset, many=True)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def retrieve(self, request, *args, **kwargs):
#         instance = self.get_object()
#         serializer = self.get_serializer(instance)
#         return Response({"rescode": 200, "resmsg": serializer.data})


# class AllinpayMembermoneyListViewSet(viewsets.ModelViewSet):
#     __title__ = "会员钱包余额"
#     __group__ = "会员模块"
#     __info__ = {"get": "查询会员钱包余额列表或详情"}
#     __doc__ = f"""
#     list:\n
#     <h4>会员钱包余额列表</h4>
#     retrieve:\n
#     <h4>会员钱包余额信息</h4>
#     """
#     http_method_names = ["get"]
#     authentication_classes = [MyJWTAuthentication, BoxJWTAuthentication]
#     permission_classes = [MyPermissions | BoxPermissions]
#     serializer_class = AllinpayMembermoneyListSerializer
#     queryset = AllinpayMembermoneyList.objects.all()

#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         serializer = self.get_serializer(queryset, many=True)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def retrieve(self, request, *args, **kwargs):
#         instance = self.get_object()
#         serializer = self.get_serializer(instance)
#         return Response({"rescode": 200, "resmsg": serializer.data})


# class AllinpayMembermoneyLogsViewSet(viewsets.ModelViewSet):
#     __title__ = "会员钱包日志"
#     __group__ = "会员模块"
#     __info__ = {"get": "查询会员钱包日志列表或详情"}
#     __doc__ = f"""
#     list:\n
#     <h4>会员钱包日志列表</h4>
#     retrieve:\n
#     <h4>会员钱包日志信息</h4>
#     """
#     http_method_names = ["get"]
#     authentication_classes = [MyJWTAuthentication, BoxJWTAuthentication]
#     permission_classes = [MyPermissions | BoxPermissions]
#     serializer_class = AllinpayMembermoneyLogsSerializer
#     queryset = AllinpayMembermoneyLogs.objects.all()

#     filter_backends = [ListDjangoFilterBackend, SearchFilter]
#     filterset_fields = ["mid", "localid"]
#     search_fields = ["reqsn"]

#     def list(self, request, *args, **kwargs):
#         queryset = self.filter_queryset(self.get_queryset())
#         serializer = self.get_serializer(queryset, many=True)
#         return Response({"rescode": 200, "resmsg": serializer.data})

#     def retrieve(self, request, *args, **kwargs):
#         instance = self.get_object()
#         serializer = self.get_serializer(instance)
#         return Response({"rescode": 200, "resmsg": serializer.data})


# =============================================
# 凡商会员数据
# =============================================
# class BMemberLoginViewSet(viewsets.ModelViewSet):
#     __title__ = "会员终端登录"
#     __group__ = "会员模块"
#     __info__ = {
#         "get": "会员终端登录查询",
#         "post": "会员终端登录创建",
#         "put": "会员终端登录编辑",
#         "delete": "会员终端登录移除",
#     }
#     __doc__ = """
#     list:
#     <h4>会员终端登录列表</h4>
#     retrieve:
#     <h4>会员终端登录详情</h4>
#     create:
#     <h4>会员终端登录创建</h4>
#         data= {
#             "name":"真实姓名",
#             "idcode" :"身份证号",
#             "phone":"手机号码",
#             "nicename":"昵称",
#             "avatar":"头像",
#             "email":"电子邮箱",
#             "openid":"微信OpenID",
#             "unid":"微信UnID",
#             "hash":"校验码",
#             "gender":"性别",
#         }
#     update:
#     <h4>会员终端登录编辑</h4>
#     delete:
#     <h4>会员终端登录移除</h4>"""
#     http_method_names = ["post"]
#     authentication_classes = []
#     permission_classes = [AllPermissions]
#     serializer_class = BMemberLoginSerializer
#     queryset = None

#     @transaction.atomic
#     def create(self, request, *args, **kwargs):
#         serializer = self.serializer_class(data=request.data)
#         serializer.is_valid(raise_exception=True)
#         # serializer.save()
#         # 会员信息
#         # vip = serializer.data.get("data")
#         # vipSer = BMemberListSerializer(data.vip)
#         # vipSer.is_valid(raise_exception=True)
#         x = serializer.data.get("data").copy()
#         x["creatime"] = timezone.now()
#         hash = x.pop("hash")
#         faq, sess = BMemberList.objects.update_or_create(defaults=x, hash=hash)
#         logger.info(
#             {
#                 "id": 1,
#                 "mid": sess.id,
#                 "pid": serializer.data.get("pid"),
#                 "pcname": serializer.data.get("pcname"),
#                 "msg": "会员终端登录",
#             }
#         )
#         return Response({"rescode": 200, "resmsg": "Success"})


# 检查视图
filter_views(globals())
